package com.lovo.bbs.servlet.admin;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.TopicBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;
import com.lovo.bbs.vo.TopicVo;

/**
 * 管理主题
 * 
 * @author tiancen2001
 * 
 */
public class TopicUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 3673014882985854438L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		int opType = 0;//操作类型
		if (request.getParameter("opType") != null) {
			opType = Integer.parseInt(request.getParameter("opType"));
		}
		
		//主题相关属性
		int topicid = Integer.parseInt(request.getParameter("topicid"));
		int forumid = Integer.parseInt(request.getParameter("forumid"));
		String topicTitle = new String(request.getParameter("topictitle").getBytes("ISO-8859-1"), "utf-8");
	
		int hits = Integer.parseInt(request.getParameter("hits"));
		int postNum=Integer.parseInt(request.getParameter("replynum"));//回复数
		
		int isHighlight = Integer.parseInt(request.getParameter("ishighlight"));//高亮显示否
		int isBest = Integer.parseInt(request.getParameter("isbest"));
		int isTop = Integer.parseInt(request.getParameter("istop"));
		
		int lastforumid = Integer.parseInt(request.getParameter("lastforumid"));//原来的论坛ID
		
		// 原页码,返回原页面时使用
		int queryPage = Integer.parseInt(request.getParameter("queryPage"));
		if (queryPage == 0) {
			queryPage = 1;
		}
		
		//组装
		TopicVo vo = new TopicVo();
		vo.setTopicid(topicid);
		vo.setForumid(forumid);
		vo.setTopicTitle(topicTitle);
		vo.setHits(hits);
		vo.setPostNum(postNum);
		vo.setHighlight(isHighlight==1?true:false);
		vo.setBest(isBest==1?true:false);
		vo.setTop(isTop==1?true:false);	
		
		int dataChanged = 0;
		ServletContext context = request.getSession().getServletContext();
		
		ForumBo forumBo = new ForumBo();
		ForumStatBo forumStatBo = new ForumStatBo();
		TopicBo topicBo = new TopicBo();
		if(opType==2){//更新主题
			dataChanged = topicBo.updateTopic(vo, TopicBo.OPER_OF_UPDATE);
			//更新论坛数据库
			forumBo.reset(forumid);
			forumBo.calcTodayTopicNum();//刷新所有论坛今日主题数
			 if(lastforumid!=forumid){
				 forumBo.reset(lastforumid);
			 }
		}else if(opType==3){//删除主题
			dataChanged = topicBo.updateTopic(vo, TopicBo.OPER_OF_DELETE);
			//刷新所有论坛今日主题数
			forumBo.calcTodayTopicNum();
			//更新论坛数据库
			dataChanged = forumStatBo.reset();
			//更新网站信息数据库
			forumBo.reset(forumid);
		}
		
		// 取得所有论坛列表
		ArrayList<ForumVo> forumList = forumBo.getAllForum();
		// 更新论坛列表
		context.setAttribute("forumList", forumList);
		// 更新论坛MAP
		context.setAttribute("forumMap", forumBo.getForumMap(forumList));

		if (opType == 3) {// 如果是删除主题,则同时更新......
			// 更新网站杂项信息
			ForumStatVo forumInfo = forumStatBo.getForumInfo();
			context.setAttribute("forumInfo", forumInfo);
		}
		
		if (dataChanged == 1) {
			resp
					.sendRedirect("topicManageQueryPage.jsp?queryPage="
							+ queryPage);
		}
		
	}

}
